import random
import math
from pathlib import Path
from PIL import Image


class RandomOcclusion(object):
    """ 
    Args:
         width_l: Minimum proportion of walker image's width against input image.
         width_h: Maximum proportion of walker image's width against input image.
         walker_l: Minimum of walker images.
         walker_h: Maximum of walker images.
         walker_path: walker images path.
    """

    def __init__(self, width_l=0.2, width_h=0.5, walker_l=0, walker_h=4, walker_path='datasets/walker'):
        self.width_l = width_l
        self.width_h = width_h
        self.walker_l = walker_l
        self.walker_h = walker_h
        self.walker_path = walker_path
    
    def cover_walker(self, walker, image):
        # 获取行人和原图的长宽
        walker_width, walker_height = walker.size
        image_width, image_height = image.size
        # 获取行人图片的比例
        walker_ratio = walker_height/walker_width
        # 获取行人图片缩放后的长宽
        cover_width = int(random.uniform(self.width_l, self.width_h) * image_width)
        cover_height = int(cover_width * walker_ratio)
        # 缩放行人图片
        walker = walker.resize((cover_width, cover_height))
        # 随机行人图片位置
        x = random.randrange(-cover_width, image_width)
        y = random.randrange(-cover_height, image_height)
        # 覆盖
        image.paste(walker, (x, y), walker)

    def __call__(self, img):

        walker_list = [Image.open(walker) for walker in Path(self.walker_path).glob('*')]
        for _ in range(random.randint(self.walker_l, self.walker_h)):
            self.cover_walker(random.choice(walker_list), img)

        return img
    
class RandomErasing(object):
    """ Randomly selects a rectangle region in an image and erases its pixels.
        'Random Erasing Data Augmentation' by Zhong et al.
        See https://arxiv.org/pdf/1708.04896.pdf
    Args:
         probability: The probability that the Random Erasing operation will be performed.
         sl: Minimum proportion of erased area against input image.
         sh: Maximum proportion of erased area against input image.
         r1: Minimum aspect ratio of erased area.
         mean: Erasing value.
    """

    def __init__(self, probability=0.5, sl=0.02, sh=0.4, r1=0.3, mean=(0.4914, 0.4822, 0.4465)):
        self.probability = probability
        self.mean = mean
        self.sl = sl
        self.sh = sh
        self.r1 = r1

    def __call__(self, img):

        if random.uniform(0, 1) >= self.probability:
            return img

        for attempt in range(100):
            area = img.size()[1] * img.size()[2]

            target_area = random.uniform(self.sl, self.sh) * area
            aspect_ratio = random.uniform(self.r1, 1 / self.r1)

            h = int(round(math.sqrt(target_area * aspect_ratio)))
            w = int(round(math.sqrt(target_area / aspect_ratio)))

            if w < img.size()[2] and h < img.size()[1]:
                x1 = random.randint(0, img.size()[1] - h)
                y1 = random.randint(0, img.size()[2] - w)
                if img.size()[0] == 3:
                    img[0, x1:x1 + h, y1:y1 + w] = self.mean[0]
                    img[1, x1:x1 + h, y1:y1 + w] = self.mean[1]
                    img[2, x1:x1 + h, y1:y1 + w] = self.mean[2]
                else:
                    img[0, x1:x1 + h, y1:y1 + w] = self.mean[0]
                return img

        return img
